<template>
  <div v-loading="loading" style="height: 100%">
    <van-nav-bar :right-text="postingDate"
                 left-arrow left-text="返回"
                 title="盘点"
                 @click-left="goBack"
                 @click-right="setPostDate"/>
    <van-cell-group :v-model="tableData"
                    style="overflow:
                    scroll;
                    height: 520px">
      <van-field ref="code_input"
                 v-model="code_input"
                 left-icon="scan"
                 placeholder="请扫描条码"
                 @input="onSubmitScanResult"/>
      <van-divider content-position="left" style="height: 2px; font-style: italic">信息</van-divider>
      <van-row style="border: solid 1px #3793fb; border-radius: 5px">
        <van-col span="24">
          <el-autocomplete id="autocomplete" v-model="tableData.location" :fetch-suggestions="storageLocationSearch"
                           :rules="{
                      required: true,
                      message: '请选择库位',
                      trigger: 'blur',
                    }" label="库位" placeholder="库位" style="width: 100%;"
                           value-key="codeanddes"></el-autocomplete>
        </van-col>
      </van-row>
      <van-field v-model="tableData.materialno" label="物料编码" readonly/>
      <van-field v-model="tableData.materialdes" label="物料描述" readonly/>
      <van-field v-model="tableData.batchno" label="批号" readonly/>
      <van-field v-model="tableData.packinglotno" label="包装批" label-width="40%" readonly/>
      <van-field v-model="tableData.productionlotno" label="生产批" label-width="40%" readonly/>
      <van-field v-model="tableData.supplierbatchno" label="供应商批次" label-width="40%" readonly/>
      <van-field v-model="tableData.locquantstatus" label="数量" label-width="40%" readonly/>
      <van-field v-model="tableData.numberofbarrels" label="桶号" label-width="40%" readonly/>
      <van-field v-model="tableData.countquantity" label="实盘数量" label-width="40%"/>
      <van-field v-if="dc_flag == 'X'" ref="code_input" v-model="th_input"
                 left-icon="scan" placeholder="桶号扫描" style="border: solid 1px #3793fb; border-radius: 5px"
                 @input="onScanBarrelCode"/>
      <van-field v-if="th_flag == 'X'" v-model="tableData.scanbucket" label="实际扫入桶号" readonly></van-field>
      <van-field v-if="th_flag == 'X'" v-model="tableData.specification" label="每桶数量" readonly></van-field>
      <van-field v-if="th_flag == 'X'" v-model="tableData.realbucket" label="实际桶号" readonly></van-field>
      <van-field v-if="th_flag == 'X'" v-model="tableData.realquantity" label="实际数量" readonly></van-field>
    </van-cell-group>
    <van-button block
                style="position: fixed; bottom: 0px; border: #3793fb solid 1px; border-radius: 2px; background-color: #3793fb; line-height: var(--footer-height)"
                type="primary" @click="onSubmit">保存
    </van-button>
  </div>
</template>

<script>

export default {
  data() {
    return {
      activeNames: ['1'], // 默认展开第一项
      supplierSearchListPart: [],
      plantList: [],
      tableData: {},
      storageLocationList: [],
      loading: false,
      showKeyboards: false,
      showCalendar: false,
      postingDate: this.getNowFormatDate(new Date()),
      activeName: '1',
      code_input: '',
      th_input: '',
      show: false,
      th_flag: false,
      dc_flag: false
    };
  },
  mounted() {
    //获取库位List
    this.getStorageLocationList();
  },
  methods: {
    onSubmitScanResult(event) {
      this.getBucket(event, 1);
    },
    onScanBarrelCode(event) {
      this.getBucket(event, 2);
    },
    async getBucket(bucketNo, type) {
      if (1 === type) {
        if (this.tableData.location === undefined) {
          this.$message({
            message: "请先输入库位在进行扫码",
            type: "error",
          });
          this.code_input = '';
          return
        }
        let scanInfo = {
          code: bucketNo + ',' + localStorage.getItem("plant"),
          location: this.tableData.location,
        };
        const {data: res} = await this.$http.post(
            "/GetLocationCheck",
            scanInfo
        );
        this.code_input = '';
        this.tableData = res[0];
        this.dc_flag = res[0].zifdc;
      } else {
        //扫描桶
        let scanInfo = {
          code: bucketNo,
        };
        const {data: res} = await this.$http.post(
            "/ProductionVerifyBucket",
            scanInfo
        );
        if (res.length && res[0].bucketnumber) {
          var requestData = this.tableData;
          for (var i in requestData.bucket) {
            if (requestData.bucket[i].numberOfBarrels === res[0].bucketnumber) {
              this.$message({
                message: "扫描失败，请勿重复扫描同一桶号",
                type: "error",
              });
              this.th_input = '';
              return
            }
          }
          if ('占用' !== res[0].bucketstatus) {
            this.$toast.fail('条码信息错误,非占用状态');
            this.th_input = '';
            return
          }
          var bucketnumber = this.tableData.realbucket;
          if (bucketnumber !== undefined && bucketnumber !== null) {
            if (bucketnumber.includes(res[0].bucketnumber)) {
              this.$toast.fail('扫描失败，请勿重复扫描同一桶号');
              this.th_input = '';
              return
            }
          }
          if (this.tableData.realbucket === undefined || this.tableData.realbucket === null || this.tableData.realbucket === "") {
            this.tableData.realbucket = res[0].bucketnumber;
            this.tableData.realquantity = this.tableData.specification;
          } else {
            this.tableData.realbucket = this.tableData.realbucket + '/' + res[0].bucketnumber;
            this.tableData.realquantity = parseInt(this.tableData.specification) + parseInt(this.tableData.realquantity);
          }
          if (this.tableData.countquantity === undefined || this.tableData.countquantity === null || this.tableData.countquantity === "") {
            this.tableData.countquantity = this.tableData.specification;
          }else {
            this.tableData.countquantity = parseInt(this.tableData.countquantity) + parseInt(this.tableData.specification);
          }
          this.tableData.scanbucket = res[0].bucketnumber;
          this.th_flag = 'X';
          this.th_input = '';
        }
      }
    },
    //获取库位List
    async getStorageLocationList() {
      let scanInfo = {
        plant: localStorage.getItem("plant"),
      };
      const {data: res} = await this.$http.post(
          "/StorageLocationList",
          scanInfo
      );
      this.storageLocationList = res;
    },
    setPostDate() {
      this.showCalendar = true;
    },
    //库位搜索帮助
    storageLocationSearch(queryString, cb) {
      var storageLocationList = this.storageLocationList;
      var results = queryString
          ? storageLocationList.filter(this.createFilter(queryString))
          : storageLocationList;
      // 调用 callback 返回建议列表的数据
      cb(results);
    },
    createFilter(queryString) {
      return (storageLocation) => {
        return (
            storageLocation.codeanddes
                .toLowerCase()
                .indexOf(queryString.toLowerCase()) !== -1
        );
      };
    },
    //保存
    onSubmit() {
      this.loading = true;
      setTimeout(() => {
        this.loading = false;
        this.postLocationCheck();
      }, 1000);
      this.dialogVisible = false;
    },//过账接口
    async postLocationCheck() {
      var requestData = this.tableData;
      console.log("requestData", requestData);
      console.log("localStorage", localStorage);
      let inputInfo = {
        location: this.tableData.location,
        materialNo: this.tableData.materialno,
        plant: requestData.plant.split("|")[0],
        batchNo: this.tableData.batchno,
        materialDes: this.tableData.materialdes,
        packingLotNo: this.tableData.packinglotno,
        productionLotNo: this.tableData.productionlotno,
        supplierBatchNo: this.tableData.supplierbatchno,
        locationStatus: this.tableData.locationstatus,
        locationQuant: this.tableData.locationquant,
        locQuantStatus: this.tableData.locquantstatus,
        numberofbarrels: this.tableData.numberofbarrels,
        countQuantity:   this.tableData.countquantity,
        scanBucket: this.tableData.scanbucket,
        specification: this.tableData.specification,
        realBucket: this.tableData.realbucket,
        realQuantity: this.tableData.realquantity,
        zifdc: this.tableData.zifdc,
        username: localStorage.getItem("erpId"),
        postdate: this.postingDate,
        // location: requestData.location.split("|")[0],

      };
      console.log("inputInfo", inputInfo)
      const {data: res} = await this.$http.post(
          "/PostLocationCheck",
          inputInfo
      );
      var response = res[0];
      if (response.type == "S") {
        this.$toast.success(response.message);
        setTimeout(() => {
          this.$router.push("/HomePage");
        }, 1000);
      } else if (response.type == "E") {
        this.$message({
          message: response.message,
          type: "error",
        });
      } else {
        this.$message({
          message: "入库失败，请联系系统维护人员",
          type: "error",
        });
      }
    },
    //返回
    goBack() {
      this.$router.push('/HomePage');
    },
  }
}
;
</script>
<style></style>
